//
// Created by Administrator on 2023/11/21.
//
#include "bits/stdc++.h"

using namespace std;
const int N = 1e5 + 10;

int main() {
    int n;
    cin >> n;
    priority_queue<int, vector<int>, greater<int>> heap;//小根堆

    for (int i = 0; i < n; ++i) {
        int x;
        cin>>x;
        heap.push(x);
    }
    int res=0;
    while(heap.size()>1){
        int a=heap.top();heap.pop();
        int b=heap.top();heap.pop();
        res+=a+b;
        heap.push(a+b);
    }
    cout<<res;

}